﻿using UnityEngine;
using System.Collections;
using TileMapClasses;

namespace TileMapClasses
{
	/// <summary>
	/// Selector for the tiles in the overworld.
	/// </summary>
	public class selector : MonoBehaviour
	{
		private int displayID;
		Vector3 pos1;
		Vector3 pos2;
		Vector3 offset;
		float moveSpeed = .5f;
		Vector3 moveTo;

		void Start ()
		{
			Tile.tileSelected += tileSelectedListener;

			//Move off screen.
			transform.position = new Vector3 (-99, -99, -99);
			pos1 = new Vector3 (-99, -99, -99);
			pos2 = new Vector3 (-99, -99, -99);
			offset = new Vector3 (-99, -99, -99);
			moveTo = new Vector3 (-99, -99, -99);
		
		}

		/// <summary>
		/// Listener for when the tiles get selected.
		/// </summary>
		/// <param name="state">If set to <c>true</c> state.</param>
		/// <param name="id">Identifier.</param>
		private void tileSelectedListener (bool state, int id)
		{
			if (state) {
				transform.position = tileManager.Instance [id].potatas.transform.position;
				Vector3 tempPos = transform.position;
				tempPos.y += 1;
				transform.position = tempPos;
				pos1 = transform.position;
				offset = Vector3.down;
				pos2 = pos1 + offset;
			} else {
				transform.position = new Vector3 (-99, -99, -99);
				pos1 = new Vector3 (-99, -99, -99);
				pos2 = new Vector3 (-99, -99, -99);
				offset = new Vector3 (-99, -99, -99);
				moveTo = new Vector3 (-99, -99, -99);
			}
		}

		void OnDestroy ()
		{
			Tile.tileSelected -= tileSelectedListener;
		}
		// Update is called once per frame
		void Update ()
		{
			if (transform.position == pos1) {
				moveTo = pos2;
			}

			if (transform.position == pos2) {
				moveTo = pos1;
			}
			transform.position = Vector3.MoveTowards (transform.position, moveTo, moveSpeed * Time.deltaTime);
			transform.Rotate (Vector3.up * Time.deltaTime * 100);
		}
	}
}
